package com.echo.code.spring.web;

import com.alibaba.fastjson.JSONObject;
import com.echo.code.spring.cache.UserService;
import com.echo.code.spring.tx.User;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.WebRequest;

import javax.servlet.http.HttpServletRequest;

/**
 * Description
 *
 * @author baiyp
 * @version 1.0.0
 * @date 2019/04/22
 */
@RestController
@RequestMapping(value = "/user")
@Log4j
public class UserController {

  @Autowired
  @Qualifier(value = "service")
  private UserService service;

  @RequestMapping(value = "/get/{userId}")
  public String getUserById(@PathVariable("userId") int id, HttpServletRequest servletRequest, WebRequest webRequest) {
    log.info("servletRequest: " + servletRequest);
    log.info("webRequest: " + webRequest);
    User user = service.getUserById(id);
    doSomething();
    return JSONObject.toJSONString(user);
  }

  private void doSomething() {
    throw new RuntimeException("Something unexpected happened.");
  }
}
